import { get, post } from '@/utils/request.js';

export function getSysSetting() {
  // return mock({ ...data, mockData: { token: 'mock-token' } })
  return get('/api_miniapp/i9_eqconfig_list');
}
export function loginpclog_list(data) {
  // return mock({ ...data, mockData: { token: 'mock-token' } })
  return post('/api_miniapp/i22_loginpclog_list', data);
}
export function getQrcodebytype(eqType) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    eqCreatedBy: user.id,
    userId: user.id,
    eqType,
  };
  return post(
    '/api_miniapp/i3_qrcode_list',

    data,
  );
}
export function templatecategory_list() {
  const data = {
    cpId: '0',
  };
  return post(
    '/api_miniapp/i5_templatecategory_list',

    data,
  );
}
export function tradecategory_list() {
  const data = {
    cpId: '0',
  };
  return post(
    '/api_miniapp/i8_tradecategory_list',

    data,
  );
}
export function tempalte_list(data) {
  // console.log('data', data);
  return post('/api_miniapp/i11_tempalte_list', data);
}
export function template_saveone(data) {
  return post('/api_miniapp/i10_template_saveone', data);
}
function formatDateTime(date) {
  function pad(value) {
    return value < 10 ? `0${value}` : value;
  }
  console.log(
    `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(
      date.getMinutes(),
    )}:${pad(date.getSeconds())}`,
  );
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(
    date.getMinutes(),
  )}:${pad(date.getSeconds())}`;
}
export function saveQrcode(data) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  data.eqCreatedBy = user.id;
  data.eqCreatedTime = formatDateTime(new Date());
  data.eqUpdatedBy = user.id;
  data.eqUpdatedTime = formatDateTime(new Date());
  data.deleteId = 0;
  return post(
    '/api_miniapp/i2_one_qrcode_save',

    data,
  );
}
export function getQrcode() {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  console.log('data', user);
  const data = {
    eqCreatedBy: user.id,
  };

  return post('/api_miniapp/i3_qrcode_list', data);
}
export function delQrcode(data) {
  data.deleteId = data.id;

  return post('/api_miniapp/i2_one_qrcode_save', data);
}

export function eqrecord_list(data) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  data.userId = user.id;
  return post('/api_miniapp/i20_eqrecord_list', data);
}
export function i12_template_one(id) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    id,
    userId: user.id,
  };
  return post('/api_miniapp/i12_template_one', data);
}

export function qrcode_one(id) {
  const data = {
    id,
  };
  return post(
    '/api_miniapp/i4_qrcode_one',

    data,
  );
}
function generateUniqueStringWithUserIdAndTimestamp(userId, length) {
  const timestamp = new Date().getTime().toString();
  let uniqueString = userId + timestamp;
  const randomString = Math.random()
    .toString(36)
    .substr(2, length - uniqueString.length);
  uniqueString += randomString;
  console.log('uniqueString', uniqueString);
  return uniqueString;
}
function random_string(len) {
  const strLeng = len || 32;
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  const maxPos = chars.length;
  let pwd = '';
  for (let i = 0; i < strLeng; i++) {
    pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}

// 获取文件后缀
function get_suffix(filename) {
  const pos = filename.lastIndexOf('.');
  let suffix = '';
  if (pos != -1) {
    suffix = filename.substring(pos);
  }
  return suffix;
}
export function getOrSetLocalStorage(key) {
  // 检查localStorage中是否存在指定的key
  const defaultValue = generateUniqueStringWithUserIdAndTimestamp(JSON.parse(localStorage.getItem('userinfo')).id, 24);

  if (localStorage.getItem(key) === '') {
    // 如果不存在，使用defaultValue设置项
    localStorage.setItem(key, defaultValue);
    return defaultValue;
  } else {
    // 如果存在，读取并返回该项的值
    return localStorage.getItem(key);
  }
}
function classifyFileExtension(ext) {
  // 将后缀统一转换为小写，以避免大小写敏感问题
  ext = ext.toLowerCase();

  // 根据后缀分类
  switch (ext) {
    case 'jpg':
    case 'jpeg':
    case 'png':
    case 'gif':
    case 'bmp':
    case 'tiff':
    case 'webp':
    case 'ico':
      return 'img'; // 图像文件

    case 'mp4':
    case 'avi':
    case 'mov':
    case 'wmv':
    case 'flv':
    case 'mkv':
      return 'video'; // 视频文件

    case 'mp3':
    case 'wav':
    case 'aac':
    case 'ogg':
      return 'audio'; // 音频文件

    case 'pdf':
    case 'doc':
    case 'docx':
    case 'xls':
    case 'xlsx':
    case 'ppt':
    case 'pptx':
    case 'txt':
      return 'doc'; // 文档文件

    default:
      return 'unknown'; // 未知文件类型
  }
}
export function uploadfile_saveone(fileId, url, size, tableRecid) {
  console.log('url:', url, JSON.parse(localStorage.getItem('userinfo')));
  const data = {
    fileId,
    tableRecid,

    tableDbname: 'fy_eqcode',
    tableRandom: getOrSetLocalStorage('groupid'),
    companyId: '1',
    fileUrl: url,
    fileType: classifyFileExtension(url.split('/').pop().split('.').pop()),
    fileSize: size,
    userId: JSON.parse(localStorage.getItem('userinfo')).id,
  };
  console.log('data:', data);

  return post(
    '/api_miniapp/i26_uploadfile_saveone',

    data,
  );
}
export function i52_uploadedfile_list() {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    userId: user.id,
  };
  return post(
    '/api_miniapp/i52_uploadedfile_list',

    data,
  );
}
export function loginpclog(data) {
  console.log('data', data);
  return post(
    '/api_miniapp/i21_loginpclog_saveone',

    data,
  );
}
export function i46_toptpcategory_list(limitNum) {
  const data = {
    limitNum,
  };
  return post(
    '/api_miniapp/i46_toptpcategory_list',

    data,
  );
}

export function getQrcodebyid(id) {
  const data = {
    id,
  };
  return post(
    '/api_miniapp/i4_qrcode_one',

    data,
  );
}

export function i59_eqcode_deletelist() {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    userId: user.id,
  };
  return post(
    '/api_miniapp/i59_eqcode_deletelist',

    data,
  );
}
export function i54_eqcode_recover(id) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    id,
    userId: user.id,
  };
  return post(
    '/api_miniapp/i54_eqcode_recover',

    data,
  );
}
export function i15_favorites_list() {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    fvType: 0,
    userId: user.id,
  };
  return post(
    '/api_miniapp/i15_favorites_list',

    data,
  );
}
export function i15_favorites_list2() {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    fvType: 1,
    userId: user.id,
  };
  return post(
    '/api_miniapp/i15_favorites_list',

    data,
  );
}

export function i57_codescanrecord_report(index) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const type = ['yestoday', 'today', 'day30', 'year1'];

  const data = { reportType: type[index], userId: user.id };
  return post(
    '/api_miniapp/i57_codescanrecord_report',

    data,
  );
}
export function i62_ipregion_scancode(index) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const type = ['yestoday', 'today', 'day30', 'year1'];

  const data = { reportType: type[index], userId: user.id };
  return post(
    '/api_miniapp/i62_ipregion_scancode',

    data,
  );
}
export function i44_getuserinfo() {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    userId: user.id,
  };
  return post(
    '/api_miniapp/i44_getuserinfo',

    data,
  );
}
export function i14_favorite_one3(id) {
  const data = {
    id: 0,
    ftId: id,
    fvType: 1,
    userId: JSON.parse(localStorage.getItem('userinfo')).id,
  };
  return post(
    '/api_miniapp/i14_favorite_one',

    data,
  );
}
export function userinfo_saveone(data) {
  console.log('data', data);
  return post(
    '/api_miniapp/i18_userinfo_saveone',

    data,
  );
}
export function i50_companyinfo_one(companyId) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {};
  data.companyId = companyId;

  data.userId = user.id;
  return post(
    '/api_miniapp/i50_companyinfo_one',

    data,
  );
}

export function i32_staff_list(data) {
  const user = JSON.parse(localStorage.getItem('userinfo'));

  data.userId = user.id;

  return post(
    '/api_miniapp/i32_staff_list',

    data,
  );
}
export function i33_staffaccount_list(data) {
  return post(
    '/api_miniapp/i33_staffaccount_list',

    data,
  );
}
export function i34_usercheck_one(userPhone) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  const data = {
    userId: user.id,
    userPhone,
  };
  return post(
    '/api_miniapp/i34_usercheck_one',

    data,
  );
}
export function i31_staff_saveone(data) {
  const user = JSON.parse(localStorage.getItem('userinfo'));

  data.adminUserid = user.id;
  data.creatorId = user.id;

  return post(
    '/api_miniapp/i31_staff_saveone',

    data,
  );
}

export function companyinfo_one(data) {
  console.log('data', data);
  return post(
    '/api_miniapp/i24_agentapply_one',

    data,
  );
}
export function agentapply_saveone(data) {
  const user = JSON.parse(localStorage.getItem('userinfo'));
  data.userId = user.id;

  return post(
    '/api_miniapp/i23_agentapply_saveone',

    data,
  );
}

export function agent_shareqrcode() {
  console.log('222:');
  const user = JSON.parse(localStorage.getItem('userinfo'));
  return get(`/api_miniapp/i27_agent_shareqrcode?userid=${user.id}&path=pages/login/phone/index`);
}
export function i30_viptmp_list(data) {
  return post(
    '/api_miniapp/i30_viptmp_list',

    data,
  );
}

